﻿using BotTalkLibrary;
using GalaSoft.MvvmLight;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Tasks;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using AllBots.i18n;
using GalaSoft.MvvmLight.Messaging;
using AllBots.Messaging;

namespace AllBots.ViewModel
{
    public class WelcomeViewModel : ViewModelBase
    {
        #region INPC

        public const string NotifyPropertyName = "Notify";
        private bool notify = false;
        public bool Notify
        {
            get
            {
                return notify;
            }

            set
            {
                if (notify == value)
                {
                    return;
                }

                var oldValue = notify;
                notify = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(NotifyPropertyName);
            }
        }

        public const string NotificationTextPropertyName = "NotificationText";
        private string notificationText = "";
        public string NotificationText
        {
            get
            {
                return notificationText;
            }

            set
            {
                if (notificationText == value)
                {
                    return;
                }

                var oldValue = notificationText;
                notificationText = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(NotificationTextPropertyName);
            }
        }

        public const string IsApplicationBarVisiblePropertyName = "IsApplicationBarVisible";
        private bool isApplicationBarVisible = false;
        public bool IsApplicationBarVisible
        {
            get
            {
                return isApplicationBarVisible;
            }

            set
            {
                if (isApplicationBarVisible == value)
                {
                    return;
                }

                var oldValue = isApplicationBarVisible;
                isApplicationBarVisible = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(IsApplicationBarVisiblePropertyName);
            }
        }

        #endregion

        #region Commands

        private RelayCommand reviewCommand;
        public ICommand ReviewCommand
        {
            get
            {
                if (reviewCommand == null)
                {
                    reviewCommand = new RelayCommand(ReviewApp);
                }

                return reviewCommand;
            }
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the WelcomeViewModel class.
        /// </summary>
        public WelcomeViewModel()
        {
            if (IsInDesignMode)
            {
                notificationText = "Design test text!";
            }
            else
            {
                // Show the tip if it is the first launch
                bool isFirstLaunch;
                if (IsolatedStorageSettings.ApplicationSettings.TryGetValue(App.FIRST_LAUNCH_KEY, out isFirstLaunch) && isFirstLaunch)
                {
                    ShowNotification(AppResources.WelcomeNotification + " " + AppResources.AppTitle);
                    
                    IsolatedStorageSettings.ApplicationSettings[App.FIRST_LAUNCH_KEY] = false;
                    IsolatedStorageSettings.ApplicationSettings.Save();
                }
            }

            RequestMaker req = new RequestMaker();

            Messenger.Default.Register<ApplicationBarVisibilityMessage>(this, m => IsApplicationBarVisible = m.IsVisible);
        }

        private void ShowNotification(string message)
        {
            Notify = false;
            notificationText = message;
            Notify = true;
        }

        private void ReviewApp()
        {
            MarketplaceReviewTask reviewTask = new MarketplaceReviewTask();
            reviewTask.Show();
        }
    }
}